package org.john.tool;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Created by adsame on 17-9-21.
 */
public class IqiyiFloorPriceGen {

    public static void main(String[] args) {
        File file = new File("/home/adsame/文档/iqiyi动态底价/bidfloor20190314.csv");
        File outFile = new File("/home/adsame/文档/iqiyi动态底价/bidfloor_out_20190314.csv");
        File sqlFile = new File("/home/adsame/文档/iqiyi动态底价/bidfloor_sql_20190314.sql");
        File modelFile = new File("/home/adsame/文档/iqiyi动态底价/model_20190314.txt");
        BufferedReader reader;
        BufferedWriter outWriter;
        BufferedWriter sqlWriter;
        BufferedWriter modelWriter;
        try {
            reader = new BufferedReader(new FileReader(file));
            outWriter = new BufferedWriter(new FileWriter(outFile));
            sqlWriter = new BufferedWriter(new FileWriter(sqlFile));
            modelWriter = new BufferedWriter(new FileWriter(modelFile));
            String tmpStr;
            String feature = null;
            Map<String, Set<String>> featureMap = new HashMap();
            DecimalFormat df = new DecimalFormat("##0.0");

            String str4 = "brand";
            boolean flag4 = str4.matches("\\D");
            System.out.println(flag4);
            System.out.println(Pattern.matches("[\\w]+_[5]", "brand_5"));
            while ((tmpStr = reader.readLine()) != null) {
                String[] arr = tmpStr.split(",");
                if (arr[0].equals("title")) {
                    continue;
                }
                if (arr[0].equals("key")) {
                    feature = arr[1] + arr[2];
                } else {
//                    outWriter.write(arr[0] + "," + arr[1] + "," + feature + "\n");
                    sqlWriter.write("INSERT INTO `iqiyi_industry_floor_table`(`industry`, `config`, `floor`) VALUES ('"
                            + arr[0] + "', '" + arr[2] + "', " + arr[1] + ");\n");
//                    String features[] = feature.split("_");
                    String features[] = arr[2].split("_");
                    String newStr = "\"" + features[0] + ":" + features[1] + "\" : [";
//                    if (features[1].equals("7")) {
//                        continue;
//                    }
                    Double d = (Double.valueOf(arr[1]) - 1) / 100;
                    Double d2 = d * 2;
                    if (featureMap.containsKey(newStr)) {
                        Set value = featureMap.get(newStr);
                        if (arr[0].matches("[\\w]+_[5]")) {
                            value.add(df.format(d));
                        } else {
                            value.add(df.format(d) + ", " + df.format(d2));
                        }
                    } else {
                        Set value = new HashSet();
                        if (arr[0].matches("[\\w]+_[5]")) {
                            value.add(df.format(d));
                        } else {
                            value.add(df.format(d) + ", " + df.format(d2));
                        }
                        featureMap.put(newStr, value);
                    }
                }
            }
            ArrayList<String> list = new ArrayList<>();
            for (Map.Entry e : featureMap.entrySet()) {
                StringBuffer sb = new StringBuffer("");
                sb.append(e.getKey().toString());
                Set<String> value = (Set) e.getValue();
                boolean startFlag = true;
                for (String v : value) {
                    if (!startFlag) {
                        sb.append(", ");
                    }
                    sb.append(v);
                    startFlag = false;
                }
                sb.append("]\n");
                list.add(sb.toString());
            }
            Collections.sort(list);
            for (int i = 0; i < list.size(); ++i) {
                modelWriter.write("        " + list.get(i));
            }
            outWriter.flush();
            sqlWriter.flush();
            modelWriter.flush();
            outWriter.close();
            sqlWriter.close();
            modelWriter.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
